package com.um.jdy.platform.station.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.device.entity.po.*;
import com.um.jdy.common.device.entity.vo.FeeEvPriceVO;
import com.um.jdy.common.device.entity.vo.FeeEvTimeVO;
import com.um.jdy.common.device.entity.vo.FeePlaceTimeVO;
import com.um.jdy.common.device.entity.vo.FeePlaceVO;
import com.um.jdy.common.device.events.FeeEvent;
import com.um.jdy.common.device.service.*;
import com.um.jdy.platform.station.entity.dto.FeeEvDTO;
import com.um.jdy.platform.station.entity.vo.FeeEvVO;
import com.um.jdy.rpc.enums.DeviceTypeEnum;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class FeeEvManager extends BaseManager<FeeEvPriceService> {

    @Autowired
    private FeeService feeService;

    @Autowired
    private FeeEvTimeService feeEvTimeService;

    @Autowired
    private FeePlaceTimeService feePlaceTimeService;

    @Autowired
    private FeePlaceService feePlaceService;

    @Autowired
    ApplicationContext applicationContext;

    public FeeEvVO get(String id) {
        Fee fee = feeService.getEntity("device_fee_id", id);

        List<FeeEvPrice> priceList = service.getList(new String[]{"device_fee_id"}, new String[]{id},
                new String[]{"sort_no"}, new String[]{"asc"});
        List<FeeEvPriceVO> prices = UMBeanUtils.copyBeanList(priceList,FeeEvPriceVO.class);

        List<FeeEvTime> timeList = feeEvTimeService.getList(new String[]{"device_fee_id"}, new String[]{id},
                new String[]{"start_time"}, new String[]{"asc"});
        List<FeeEvTimeVO> times = UMBeanUtils.copyBeanList(timeList,FeeEvTimeVO.class);

        List<FeePlaceTime> placeTimeList = feePlaceTimeService.getList(new String[]{"device_fee_id"}, new String[]{id},
                new String[]{"start_time"}, new String[]{"asc"});
        List<FeePlaceTimeVO> placeTimes = UMBeanUtils.copyBeanList(placeTimeList,FeePlaceTimeVO.class);

        FeePlace feePlace = feePlaceService.getEntity("device_fee_id",id);
        FeePlaceVO feePlaceVO = UMBeanUtils.copyBean(feePlace,FeePlaceVO.class);

        FeeEvVO feeEvVO = new FeeEvVO();
        feeEvVO.setName(fee.getName());
        feeEvVO.setPrices(prices);
        feeEvVO.setTimes(times);
        feeEvVO.setPlaceTimeList(placeTimes);
        feeEvVO.setPlace(feePlaceVO);
        return feeEvVO;
    }

    @Transactional
    public FeeEvVO add(FeeEvDTO feeEvDTO) {
        Fee fee = UMBeanUtils.copyBean(feeEvDTO,Fee.class);
        fee.setCreateTime(new Date());
        fee.setDeviceType(DeviceTypeEnum.EV.getKey());
        feeService.addEntity(fee);

        FeePlace feePlace = UMBeanUtils.copyBean(feeEvDTO.getPlace(),FeePlace.class);
        feePlace.setDeviceFeeId(fee.getDeviceFeeId());
        feePlaceService.add(feePlace);

        //保存新价格列表
        feeEvDTO.getPrices().stream().forEach(priceDTO -> {
            priceDTO.setDeviceFeeId(fee.getDeviceFeeId());
            service.save(BeanUtil.toBean(priceDTO, FeeEvPrice.class));
        });

        //保存新时段列表
        feeEvDTO.getTimes().stream().forEach(timeDTO -> {
            timeDTO.setDeviceFeeId(fee.getDeviceFeeId());
            feeEvTimeService.save(BeanUtil.toBean(timeDTO, FeeEvTime.class));
        });

        //保存占位时段列表
        feeEvDTO.getPlaceTimeList().stream().forEach(timeDTO -> {
            timeDTO.setDeviceFeeId(fee.getDeviceFeeId());
            feePlaceTimeService.save(BeanUtil.toBean(timeDTO, FeePlaceTime.class));
        });

        return BeanUtil.toBean(feeEvDTO, FeeEvVO.class);
    }

    @Transactional
    public FeeEvVO update(String id, FeeEvDTO feeEvDTO) {
        Fee fee = UMBeanUtils.copyBean(feeEvDTO,Fee.class);
        fee.setDeviceFeeId(id);
        fee.setUpdateTime(new Date());
        feeService.updateEntity(fee);

        feePlaceService.delete("device_fee_id",id);
        FeePlace feePlace = UMBeanUtils.copyBean(feeEvDTO.getPlace(),FeePlace.class);
        feePlace.setDeviceFeeId(id);
        feePlaceService.add(feePlace);

        //删除原价格列表
        service.delete("device_fee_id", id);
        //保存新价格列表
        feeEvDTO.getPrices().stream().forEach(priceDTO -> {
            priceDTO.setDeviceFeeId(id);
            service.save(BeanUtil.toBean(priceDTO, FeeEvPrice.class));
        });

        //删除原时段列表
        feeEvTimeService.delete("device_fee_id", id);
        //保存新时段列表
        feeEvDTO.getTimes().stream().forEach(timeDTO -> {
            timeDTO.setDeviceFeeId(id);
            feeEvTimeService.save(BeanUtil.toBean(timeDTO, FeeEvTime.class));
        });

        feePlaceTimeService.delete("device_fee_id", id);
        //保存占位时段列表
        feeEvDTO.getPlaceTimeList().stream().forEach(timeDTO -> {
            timeDTO.setDeviceFeeId(fee.getDeviceFeeId());
            feePlaceTimeService.save(BeanUtil.toBean(timeDTO, FeePlaceTime.class));
        });

        //通知更新收费方案
        ApplicationEvent event = new FeeEvent<>(this,new Date().getTime(),fee);
        applicationContext.publishEvent(event);

        return BeanUtil.toBean(feeEvDTO, FeeEvVO.class);
    }
}
